package com.bootdo.common.service.impl;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.enums.NotifyType;
import com.bootdo.enums.SubscribeStatus;
import com.bootdo.form.JzForm;
import com.bootdo.form.PbAddForm;
import com.bootdo.oa.dao.NotifyDao;
import com.bootdo.oa.dao.NotifyRecordDao;
import com.bootdo.oa.domain.NotifyDO;
import com.bootdo.oa.domain.NotifyRecordDO;
import com.bootdo.system.dao.DeptDao;
import com.bootdo.system.dao.DocPbDao;
import com.bootdo.system.dao.SubscribeRecordDao;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.domain.*;
import com.bootdo.util.DatePattern;
import com.bootdo.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DocService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private NotifyDao notifyDao;
    @Autowired
    private NotifyRecordDao notifyRecordDao;
    @Autowired
    private SubscribeRecordDao subscribeRecordDao;
    @Autowired
    private DocPbDao docPbDao;

    /**
     * 获取当日叫诊数据
     */
    public List<DocListDO> list(Long docUserId, String patientName){
        List<DocListDO> result = new ArrayList<>();
        Long patientUserId = null;
        if(StringUtils.isNotEmpty(patientName.trim())){
            // 查询患者
            UserDO byName = userDao.getByName(patientName.trim());
            if(byName != null){
                patientUserId = byName.getUserId();
            }
        }
        Date beginTime = DateUtils.getStartOfDay(new Date());
        Date endTime = DateUtils.getEndOfDay(new Date());
        // 获取预约数据
        List<SubscribeRecordDO> list = subscribeRecordDao.list3(docUserId, patientUserId, beginTime, endTime);
        for(SubscribeRecordDO item : list){
            DocListDO recordDO = new DocListDO();
            recordDO.setId(item.getId());
            recordDO.setSeeTime(item.getSeeTime());
            recordDO.setCreateTime(item.getCreateTime());
            SubscribeStatus anEnum = SubscribeStatus.getEnum(item.getStatus());
            recordDO.setStatus(anEnum == null ? "--" : anEnum.getDesc());
            // 查询患者信息
            UserDO userDO = userDao.get(item.getUserId());
            recordDO.setPatientName(userDO == null ? null : userDO.getName());
            result.add(recordDO);
        }
        return result;
    }

    /**
     * 叫诊
     */
    @Transactional
    public R jz(JzForm JzForm){
        // 查询数据是否存在
        SubscribeRecordDO recordDO = subscribeRecordDao.get(JzForm.getId());
        if(recordDO == null){
            return R.error("预约数据不存在！");
        }
        // 状态判断
        SubscribeStatus anEnum = SubscribeStatus.getEnum(recordDO.getStatus());
        if(SubscribeStatus.One != anEnum){
            String msg = "--";
            if(anEnum != null){
                msg = anEnum.getDesc();
            }
            return R.error(msg + "状态不支持叫诊！");
        }
        // 将状态改为已就诊
        SubscribeRecordDO update = new SubscribeRecordDO();
        update.setId(JzForm.getId());
        update.setStatus(SubscribeStatus.Two.getSign());
        subscribeRecordDao.update(update);
        // 获取患者信息
        UserDO userDO = userDao.get(recordDO.getUserId());
        String patientName = userDO == null ? "**" : userDO.getName();
        // 获取医生信息
        UserDO docUser = userDao.get(recordDO.getDocUserId());
        String docName = docUser == null ? "**" : docUser.getName();
        // 获取科室信息
        String deptName = "**";
        if(docUser != null){
            DeptDO deptDO = deptDao.get(docUser.getDeptId());
            if(deptDO != null && StringUtils.isNotEmpty(deptDO.getName())){
                deptName = deptDO.getName();
            }
        }
        // 发送个人指定通知
        NotifyDO notifyDO = new NotifyDO();
        notifyDO.setType(NotifyType.JiaoZhen.getSign());
        notifyDO.setTitle("就诊");
        notifyDO.setContent("请【" + patientName + "】来【" + deptName + "】科室找【" + docName + "】医生就诊。");
        notifyDO.setUpdateDate(new Date());
        notifyDO.setStatus("1");
        notifyDO.setCreateBy(ShiroUtils.getUserId());
        notifyDao.save(notifyDO);
        // 记录 notifyRecordDao
        NotifyRecordDO recordDO1 = new NotifyRecordDO();
        recordDO1.setUserId(userDO.getUserId());
        recordDO1.setIsRead(0);
        recordDO1.setNotifyId(notifyDO.getId());
        notifyRecordDao.save(recordDO1);

        return R.ok();
    }

    public List<DocPbDO> list1(Long docUserId){
        List<DocPbDO> list = docPbDao.list(docUserId);
        for (DocPbDO item : list){
            item.setStatusText(item.getStatus() == 0 ? "停诊" : "开诊");
        }
        return list;
    }

    public R tingZhen(Long id){
        DocPbDO docPbDO = docPbDao.get(id);
        if(docPbDO == null){
            return R.error("数据不存在！");
        }
        DocPbDO update = new DocPbDO();
        update.setId(id);
        update.setStatus(0);
        docPbDao.update(update);

        // 查询医生名字
        UserDO userDO = userDao.get(docPbDO.getDocUserId());
        String docName = userDO == null ? "**" : userDO.getName();

        NotifyDO notifyDO = new NotifyDO();
        notifyDO.setType(NotifyType.TingZhen.getSign());
        notifyDO.setTitle("停止就诊");
        notifyDO.setContent("【" + docName + "】医生停止就诊。");
        notifyDO.setUpdateDate(new Date());
        notifyDO.setStatus("1");
        notifyDao.save(notifyDO);
        return R.ok();
    }

    public R kaiZhen(Long id){
        DocPbDO docPbDO = docPbDao.get(id);
        if(docPbDO == null){
            return R.error("数据不存在！");
        }
        DocPbDO update = new DocPbDO();
        update.setId(id);
        update.setStatus(1);
        docPbDao.update(update);
        // 查询医生名字
        UserDO userDO = userDao.get(docPbDO.getDocUserId());
        String docName = userDO == null ? "**" : userDO.getName();

        NotifyDO notifyDO = new NotifyDO();
        notifyDO.setType(NotifyType.JiaoZhen.getSign());
        notifyDO.setTitle("开始就诊");
        notifyDO.setContent("【" + docName + "】医生开始就诊。");
        notifyDO.setUpdateDate(new Date());
        notifyDO.setStatus("1");
        notifyDao.save(notifyDO);
        return R.ok();
    }

    public R savePbAdd(Long docUserId, PbAddForm docPbDO){
        Date date = DateUtils.parse(docPbDO.getDt(), DatePattern.YMD);
        Date startTime = DateUtils.parse(docPbDO.getDt() + " " + docPbDO.getSt(), DatePattern.YMD_HM);
        Date endTime = DateUtils.parse(docPbDO.getDt() + " " + docPbDO.getEt(), DatePattern.YMD_HM);

        if(date.before(new Date())){
            return R.error("不能向前排班！");
        }
        if(startTime.after(endTime)){
            return R.error("请正确配置开始时间 结束时间！");
        }
        // 判断当日是否已经排班

        DocPbDO docPbDao1 = docPbDao.get1(docUserId, docPbDO.getDt());
        if(docPbDao1 != null){
            return R.error(docPbDO.getDt() + " 排班信息已存在！");
        }

        DocPbDO insert = new DocPbDO();
        insert.setDocUserId(docUserId);
        insert.setDate(docPbDO.getDt());
        insert.setStartTime(startTime);
        insert.setEndTime(endTime);
        insert.setNum(Integer.valueOf(docPbDO.getNum()));
        insert.setSurplus(insert.getNum());
        insert.setStatus(0);
        int save = docPbDao.save(insert);
        if(save > 0){
            return R.ok();
        }
        return R.error();
    }
}
